package com.assets.asset.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.assets.asset.domain.AssetView;
import com.assets.asset.service.ITAssetService;
import com.assets.asset.annotation.TurnDeptID;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.assets.common.annotation.Log;
import com.assets.common.core.controller.BaseController;
import com.assets.common.core.domain.AjaxResult;
import com.assets.common.enums.BusinessType;
import com.assets.asset.domain.TAsset;
import com.assets.common.utils.poi.ExcelUtil;
import com.assets.common.core.page.TableDataInfo;

/**
 * 资产列表Controller
 *
 * @author bin
 * @date 2023-03-10
 */
@RestController
@RequestMapping("/asset/asset")
public class TAssetController extends BaseController
{
    @Autowired
    private ITAssetService tAssetService;

    /**
     * 查询在用以及闲置的资产列表列表
     */
    @PreAuthorize("@ss.hasPermi('asset:asset:list')")
    @GetMapping("/list")
    public TableDataInfo list(TAsset tAsset)
    {
        startPage();
        List<TAsset> list = tAssetService.selectTAssetList(tAsset);
        return getDataTable(list);
    }

    /**
     * 查询资产已闲置的列表列表
     */
    @PreAuthorize("@ss.hasPermi('asset:asset:list')")
    @GetMapping("/listIdle")
    public TableDataInfo listIdle(TAsset tAsset)
    {
        startPage();
        List<TAsset> list = tAssetService.selectTAssetListIdle(tAsset);
        return getDataTable(list);
    }

    /**
     * 查询资产在用列表列表
     */
    @PreAuthorize("@ss.hasPermi('asset:asset:list')")
    @GetMapping("/listUse")
    public TableDataInfo listUse(TAsset tAsset)
    {
        startPage();
        List<TAsset> list = tAssetService.selectTAssetListUser(tAsset);
        return getDataTable(list);
    }


    /**
     * 获取已报废资产列表中的详细信息
     */
    @PreAuthorize("@ss.hasPermi('asset:asset:query')")
    @GetMapping(value = "/reject/{assetCode}")
    public AjaxResult getInfoScrap(@PathVariable("assetCode") String assetCode)
    {
//        AjaxResult ajax = AjaxResult.success(tAssetService.selectTAssetByAssetCode(assetCode));
//        ajax.put("scraps",tAssetService.selectTAssetByAssetCode(assetCode));
        return success(tAssetService.selectTAssetByAssetCode(assetCode));
    }

    //明天来



    /**
     * 导出资产列表列表
     */
    @PreAuthorize("@ss.hasPermi('asset:asset:export')")
    @Log(title = "资产列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TAsset tAsset)
    {
        List<TAsset> list = tAssetService.selectTAssetList(tAsset);
        ExcelUtil<TAsset> util = new ExcelUtil<TAsset>(TAsset.class);
        util.exportExcel(response, list, "资产列表数据");
    }

    /**
     * 获取资产列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('asset:asset:query')")
    @GetMapping(value = "/{aId}")
    public AjaxResult getInfo(@PathVariable("aId") Long aId)
    {
        AjaxResult ajax = AjaxResult.success(tAssetService.selectTAssetByAId(aId));
        ajax.put("assetAdmins",tAssetService.selectAdminList());
        ajax.put("assetUsers",tAssetService.selectAllUserList());
        return ajax;
    }

    /**
     * 新增资产列表
     */
    @PreAuthorize("@ss.hasPermi('asset:asset:add')")
    @Log(title = "资产列表", businessType = BusinessType.INSERT)
    @PostMapping
    @TurnDeptID
    public AjaxResult add(@RequestBody TAsset tAsset)
    {
        AjaxResult ajax = AjaxResult.success(tAssetService.insertTAsset(tAsset));
        ajax.put("assetAdmins",tAssetService.selectAdminList());
        ajax.put("assetUsers",tAssetService.selectAllUserList());
        return ajax;
    }

    /**
     * 修改资产列表
     */
    @PreAuthorize("@ss.hasPermi('asset:asset:edit')")
    @Log(title = "资产列表", businessType = BusinessType.UPDATE)
    @PutMapping
    @TurnDeptID
    public AjaxResult edit(@RequestBody TAsset tAsset)
    {
        return toAjax(tAssetService.updateTAsset(tAsset));
    }

    /**
     * 删除资产列表
     */
    @PreAuthorize("@ss.hasPermi('asset:asset:remove')")
    @Log(title = "资产列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{aIds}")
    public AjaxResult remove(@PathVariable Long[] aIds)
    {
        return toAjax(tAssetService.deleteTAssetByAIds(aIds));
    }


    /**
     * 获取资产数量
     * @return
     */
    @PreAuthorize("@ss.hasPermi('asset:asset:query')")
    @GetMapping("/listAssetNum")
    public TableDataInfo getAssetNum(AssetView assetView)
    {
//        startPage();
//        AjaxResult ajax = AjaxResult.success();
        List<AssetView> assetNum = tAssetService.getAssetNum();
        return getDataTable(assetNum);
    }


//    @PreAuthorize("@ss.hasPermi('asset:asset:query')")
//    @GetMapping("/listAssetNum")
//    public AjaxResult getAssetNum(AssetView assetView)
//    {
//        AjaxResult ajax = AjaxResult.success();
//        List<AssetView> assetNum = tAssetService.getAssetNum(assetView);
//        return ajax;
//    }



    /**
     * 获取用户选择框列表
     */
//    @PreAuthorize("@ss.hasPermi('asset:asset:query')")
//    @GetMapping(value = "/{aId}")
//    public AjaxResult getUserList()
//    {
//        List<SysUser> sysUsers = tAssetService.selectAllUserList();
//        return success(sysUsers);
//    }



}
